package com.city.java.users.controller;

import com.city.java.base.utils.PageVO;
import com.city.java.users.pojo.entity.Users;
import com.city.java.users.service.UserService;
import com.city.java.base.response.JsonResult;
import com.city.java.base.response.StatusCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户控制器
 * RestController注解: 组合注解，相当于@Controller + @ResponseBody
 * Api注解: Knife4j文档中模块的显示名称
 * RequestMapping注解: 设置统一的请求路径前缀
 */
@Slf4j
@Api(tags = "01.用户管理模块")
@RestController
@RequestMapping("/api/users")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 添加用户
     * PostMapping注解: 处理POST请求
     * ApiOperation注解: Knife4j文档中接口的显示名称
     */
    @PostMapping("/add")
    @ApiOperation(value = "添加用户", notes = "添加新用户信息")
    public JsonResult addUser(
            @ApiParam(value = "用户信息", required = true)
            @RequestBody Users user) {
        log.info("接收到添加用户请求: {}", user);
        return userService.addUser(user);
    }

    /**
     * 根据用户ID查询用户
     * GetMapping注解: 处理GET请求
     * PathVariable注解: 获取路径中的参数
     */
    @GetMapping("/select/id/{userId}")
    @ApiOperation(value = "根据ID查询用户", notes = "根据用户ID查询用户详细信息")
    public JsonResult getUserById(
            @ApiParam(value = "用户ID", required = true, example = "1")
            @PathVariable Long userId) {
        log.info("接收到根据ID查询用户请求: ID={}", userId);
        return userService.getUserById(userId);
    }

    /**
     * 根据用户名查询用户
     * RequestParam注解: 获取请求参数
     */
    @GetMapping("/select/name/username")
    @ApiOperation(value = "根据用户名查询用户", notes = "根据用户名查询用户详细信息")
    public JsonResult getUserByUsername(
            @ApiParam(value = "用户名", required = true, example = "张三")
            @RequestParam String username) {
        log.info("接收到根据用户名查询用户请求: username={}", username);
        return userService.getUserByUsername(username);
    }

    @GetMapping("/select")
    @ApiOperation(value = "根据情况查询用户信息", notes = "根据情况查询用户信息")
    public JsonResult select(Users user){
        log.debug("查询用户信息: {}", user);
        PageVO<Users> list = userService.select(user);
        return JsonResult.ok(list);
    }

    /**
     * 根据用户ID删除用户
     * DeleteMapping注解: 处理DELETE请求
     */
    @DeleteMapping("/delete/id/{userId}")
    @ApiOperation(value = "根据ID删除用户", notes = "根据用户ID删除用户")
    public JsonResult deleteUserById(
            @ApiParam(value = "用户ID", required = true, example = "1")
            @PathVariable Long userId) {
        log.info("接收到根据ID删除用户请求: ID={}", userId);
        return userService.deleteUserById(userId);
    }

    /**
     * 根据用户名删除用户
     */
    @DeleteMapping("/delete/name/username")
    @ApiOperation(value = "根据用户名删除用户", notes = "根据用户名删除用户")
    public JsonResult deleteUserByUsername(
            @ApiParam(value = "用户名", required = true, example = "张三")
            @RequestParam String username) {
        log.info("接收到根据用户名删除用户请求: username={}", username);
        return userService.deleteUserByUsername(username);
    }

    /**
     * 更新用户信息
     * PutMapping注解: 处理PUT请求
     */
    @PutMapping("/update")
    @ApiOperation(value = "更新用户信息", notes = "更新用户详细信息")
    public JsonResult updateUser(
            @ApiParam(value = "用户信息", required = true)
            @RequestBody Users user) {
        log.info("接收到更新用户信息请求: {}", user);
        return userService.updateUser(user);
    }
    // 在 UserController.java 中添加以下方法
@PutMapping("/update/points")
@ApiOperation(value = "更新用户积分", notes = "根据用户ID更新用户积分")
public JsonResult updateUserPoints(
        @ApiParam(value = "用户ID", required = true)
        @RequestParam Long userId,
        @ApiParam(value = "积分变化值", required = true)
        @RequestParam Integer points) {
    log.info("接收到更新用户积分请求: userId={}, points={}", userId, points);
    return userService.updateUserPoints(userId, points);
}


    @PostMapping("/login")
    @ApiOperation(value = "前端用户登录验证", notes = "前端用户登录验证信息")
    public JsonResult login(@RequestBody Users users){
        log.debug("用户登录验证: {}", users);
        try {
            String auth = userService.login(users);
            return JsonResult.ok(auth);
        } catch (Exception e) {
            log.error("用户登录失败: {}", e.getMessage(), e);
            return new JsonResult(StatusCode.USERNAME_PASSWORD_ERROR);
        }
    }
}